package com.cpi.cornpp.purchaseinstructiondetail.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructionsforecasts.exception.InstructionsForecastsException;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cpi.cornpp.product.converter.ProductConverter;
import com.cpi.cornpp.purchaseinstructiondetail.dto.InstructionPurchaseDetailDTO;
import com.cpi.cornpp.purchaseinstructiondetail.exception.InstructionPurchaseDetailException;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;
import com.cs.common.utils.NumberUtil;

public class InstructionPurchaseDetailConverter {
	/**
	 * PurchaseInstruction DetailException Converter DTO to View
	 * @param dto
	 * @return
	 * @throws PlantVersionException
	 */
	public InstructionPurchaseDetailView converterPurchaseInstructionDetailDTOToView(InstructionPurchaseDetailDTO dto) throws InstructionPurchaseDetailException{
		InstructionPurchaseDetailView instructionPurchaseDetailView = null;
		ProductConverter productConverter = null;
		try{
			productConverter = new ProductConverter();
			if(dto != null){
				instructionPurchaseDetailView = new InstructionPurchaseDetailView();
				instructionPurchaseDetailView.setPurchaseInstructionDetailid(dto.getPurchaseInstructionDetailid());
				instructionPurchaseDetailView.setPurchaseInstructionId(dto.getPurchaseInstructionId());
				instructionPurchaseDetailView.setRow(StringUtils.leftPad(String.valueOf(dto.getRow()), 3, "0"));
				instructionPurchaseDetailView.getCustomerView().setCustomerId(dto.getCustomerId());
				instructionPurchaseDetailView.getCustomerView().setDescription(dto.getCustomerDescription());				
				instructionPurchaseDetailView.setProductList(productConverter.convertDTOs(dto.getProductViews()));
				instructionPurchaseDetailView.setItemQuantity(NumberUtil.convertQuantity(dto.getItemQuantity()));				
				instructionPurchaseDetailView.setCornQuantity(NumberUtil.convertQuantity(dto.getCornQuantity()));
				instructionPurchaseDetailView.setFutureTypeId(dto.getFutureTypeId().trim());				
				instructionPurchaseDetailView.setInstructionStrategyId(dto.getInstructionStrategyId());
				instructionPurchaseDetailView.setInstructionTypeId(dto.getInstructionTypeId());
				instructionPurchaseDetailView.setStatus(dto.getStatus());
				instructionPurchaseDetailView.setFutures(NumberUtil.convertInteger(dto.getFutures()));
				instructionPurchaseDetailView.setTons(NumberUtil.convertQuantity(dto.getTons()));
			}			
		}catch( Exception exception ){
			InstructionPurchaseDetailException instructionPurchaseDetailException=null;
			exception.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseDetailException;
		}
		return instructionPurchaseDetailView;
	}	
	/**
	 * PurchaseInstruction Detail Converter Views To DTOs
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws PlantVersionException
	 */
	public InstructionPurchaseDetailDTO converterPurchaseInstructionDetailViewToDto(InstructionPurchaseDetailView view) throws InstructionPurchaseDetailException {
		InstructionPurchaseDetailDTO instructionPurchaseDetailDTO = null;
		double itemQuantity = 0;
		double cornQuantity = 0;
		int row = 0;
		try{
			if(view != null){
				instructionPurchaseDetailDTO = new InstructionPurchaseDetailDTO();
				instructionPurchaseDetailDTO.setPurchaseInstructionDetailid(view.getPurchaseInstructionDetailid());
				instructionPurchaseDetailDTO.setPurchaseInstructionId(view.getPurchaseInstructionId());
				row = NumberUtil.parseInt(view.getRow());
				instructionPurchaseDetailDTO.setRow(row!=-1?row:1);
				instructionPurchaseDetailDTO.setCustomerId(view.getCustomerView().getCustomerId());
				//TODO Convertir a lsitado de dtos para guardar los productos
				itemQuantity = NumberUtil.parseDouble(view.getItemQuantity());
				if( itemQuantity == -1){
					instructionPurchaseDetailDTO.setItemQuantity(0);
				}else{
					instructionPurchaseDetailDTO.setItemQuantity(itemQuantity);
				}
				cornQuantity = NumberUtil.parseDouble(view.getCornQuantity());
				if ( cornQuantity == -1 ){
					instructionPurchaseDetailDTO.setCornQuantity(0);
				}else{
					instructionPurchaseDetailDTO.setCornQuantity(cornQuantity);
				}
				instructionPurchaseDetailDTO.setFutureTypeId(view.getFutureTypeId());
				instructionPurchaseDetailDTO.setInstructionStrategyId(view.getInstructionStrategyId());
				instructionPurchaseDetailDTO.setInstructionTypeId(view.getInstructionTypeId());
				instructionPurchaseDetailDTO.setStatus(view.getStatus());
			}
		}catch( Exception exception ){
			InstructionPurchaseDetailException instructionPurchaseDetailException=null;
			exception.printStackTrace(System.out);
			instructionPurchaseDetailException= new InstructionPurchaseDetailException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseDetailException;
		}		
		return instructionPurchaseDetailDTO;
	}
	/**
	 * 
	 * @param PurchaseInstructionDetailDTOs
	 * @return
	 * @throws InstructionsForecastsException
	 */
	public List convertDTOsToViews(List PurchaseInstructionDetailDTOs) throws InstructionPurchaseDetailException {
		List instructionsForecatsDTOs = null;
		Iterator iterator = null;
		InstructionPurchaseDetailDTO InstructionPurchaseDetailDTO = null;
		InstructionPurchaseDetailView InstructionPurchaseDetailView = null;		
		try{
			instructionsForecatsDTOs = new ArrayList(0);
			iterator = PurchaseInstructionDetailDTOs.iterator();
			while(iterator.hasNext()){
				InstructionPurchaseDetailDTO = (InstructionPurchaseDetailDTO) iterator.next();
				InstructionPurchaseDetailView = converterPurchaseInstructionDetailDTOToView(InstructionPurchaseDetailDTO);								
				instructionsForecatsDTOs.add(InstructionPurchaseDetailView);			
			}			
		}catch(Exception exception){
			InstructionPurchaseDetailException instructionPurchaseDetailException=null;
			exception.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseDetailException;
		}		
		return instructionsForecatsDTOs;
	}
}
